package com.yxh.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

import com.yxh.R;
import com.yxh.activity.ClassAddByInputActivity;
import com.yxh.activity.ClassAddByQrCodeActivity;
import com.yxh.util.AppUtils;

public class HomeRightPopup implements OnClickListener, OnDismissListener {

	private Activity mActivity;

	private LayoutInflater mInflater = null;

	private PopupWindow mPopupWindow = null;

	public HomeRightPopup(Activity activity) {
		mActivity = activity;
		mInflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		initScanCodePanel();
	}

	@SuppressWarnings("deprecation")
	private void initScanCodePanel() {
		View view = mInflater.inflate(R.layout.view_home_right_pop, null);
		mPopupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, 260,
				true);
		LinearLayout scanCode = (LinearLayout) view
				.findViewById(R.id.scan_code);
		LinearLayout addClass = (LinearLayout) view
				.findViewById(R.id.add_class);
		scanCode.setOnClickListener(this);
		addClass.setOnClickListener(this);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		mPopupWindow.setTouchable(true);
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setOnDismissListener(this);

	}

	public void showScanCodePanel(View anchor) {

		mPopupWindow.showAsDropDown(anchor,
				-((int) (80 * AppUtils.getScaledDensity(mActivity))), -10);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.scan_code:
			mPopupWindow.dismiss();
			Intent it = new Intent(mActivity, ClassAddByQrCodeActivity.class);
			mActivity.startActivity(it);
			break;

		case R.id.add_class:
			mPopupWindow.dismiss();
			Intent intent = new Intent(mActivity, ClassAddByInputActivity.class);
			mActivity.startActivity(intent);
			break;
		}

	}

	@Override
	public void onDismiss() {
		mPopupWindow = null;

	}

}
